﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UITesting;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using System.Reflection;

namespace JSC.Projects.VisualStudio.TestTools.UITest.Extension.AjaxMaskedEditExtender
{
    /// <summary>
    /// This propertyprovider currently kicks in if the SearchProperties[Name] ends with "txtKPDatum"
    /// </summary>
    public class AjaxMaskEditExtenderUIPropertyProvider : Microsoft.VisualStudio.TestTools.UITesting.UITestPropertyProvider
    {
        private UITestPropertyProvider _provider;
        private readonly string[] SEARCHPROPERTYVALUES = new string[] { "txtKPDatum", "txtKPTLDate" };

        /// <summary>
        /// Use the OriginalUITestPropertyProvider to do all the normal behaviour
        /// </summary>
        internal UITestPropertyProvider OriginalUITestPropertyProvider
        {
            get
            {
                if (_provider == null)
                {
                    Type htmlPropertyProviderType = Type.GetType("Microsoft.VisualStudio.TestTools.UITesting.HtmlPropertyProvider, Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0");
                    ConstructorInfo htmlPropertyProviderConstructor = htmlPropertyProviderType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, Type.EmptyTypes, null);
                    object o = htmlPropertyProviderConstructor.Invoke(new object[] { });

                    _provider = (Microsoft.VisualStudio.TestTools.UITesting.UITestPropertyProvider)o;
                }
                return _provider;
            }
        }

        /// <summary>
        /// Determine if the control needs to handle custom ControlSpecificSupport.
        /// </summary>
        /// <param name="uiTestControl"></param>
        /// <returns></returns>
        public override int GetControlSupportLevel(UITestControl uiTestControl)
        {
            if (string.Equals(uiTestControl.TechnologyName, "Web", StringComparison.OrdinalIgnoreCase))
            {
                if (uiTestControl.ControlType == ControlType.Edit)
                {
                    string propValue = uiTestControl.SearchProperties["Name"];
                    if (!string.IsNullOrEmpty(propValue))
                    {
                        if (SEARCHPROPERTYVALUES.Any(x => propValue.EndsWith(x)))
                        {
                            return (int)ControlSupport.ControlSpecificSupport;
                        }
                    }
                }
            }

            return (int)ControlSupport.DefaultSupport;
        }

        public override string[] GetPredefinedSearchProperties(Type specializedClass)
        {
            return OriginalUITestPropertyProvider.GetPredefinedSearchProperties(specializedClass);
        }

        public override UITestPropertyDescriptor GetPropertyDescriptor(UITestControl uiTestControl, string propertyName)
        {
            return OriginalUITestPropertyProvider.GetPropertyDescriptor(uiTestControl, propertyName);
        }

        public override string GetPropertyForAction(UITestControl uiTestControl, Microsoft.VisualStudio.TestTools.UITest.Common.UITestAction action)
        {
            return OriginalUITestPropertyProvider.GetPropertyForAction(uiTestControl, action);
        }

        public override string[] GetPropertyForControlState(UITestControl uiTestControl, Microsoft.VisualStudio.TestTools.UITest.Extension.ControlStates uiState, out bool[] stateValues)
        {
            return OriginalUITestPropertyProvider.GetPropertyForControlState(uiTestControl, uiState, out stateValues);
        }

        public override ICollection<string> GetPropertyNames(UITestControl uiTestControl)
        {
            return OriginalUITestPropertyProvider.GetPropertyNames(uiTestControl);
        }

        public override Type GetPropertyNamesClassType(UITestControl uiTestControl)
        {
            return OriginalUITestPropertyProvider.GetPropertyNamesClassType(uiTestControl);
        }

        public override object GetPropertyValue(UITestControl uiTestControl, string propertyName)
        {
            return OriginalUITestPropertyProvider.GetPropertyValue(uiTestControl, propertyName);
        }

        public override Type GetSpecializedClass(UITestControl uiTestControl)
        {
            return OriginalUITestPropertyProvider.GetSpecializedClass(uiTestControl);
        }

        public override void SetPropertyValue(UITestControl uiTestControl, string propertyName, object value)
        {
            if (SEARCHPROPERTYVALUES.Any(x => uiTestControl.Name.EndsWith(x)))
            {
                string v = ((string) value).Replace("-", "");
                Keyboard.SendKeys(uiTestControl, v);
            }
            else
            {
                OriginalUITestPropertyProvider.SetPropertyValue(uiTestControl, propertyName, value);
            }
        }
    }
}
